home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / TABIFY.C < prev    next >
Text File  |  1993-06-26  |  3KB  |  121 lines

  1. /*
  2.     "Tabify"
  3.     written by Leor Zolman
  4.  
  5.     command format: A>tabify oldfile newfile
  6.  
  7.     This program takes a text file and converts
  8.     sequences of spaces into tabs wherever possible,
  9.     in order to reduce the size of the file (and the
  10.     time it takes to transmit it using TELNET.)
  11.  
  12.     There shouldn't be any control characters within
  13.     the file, except for carriage returns and linefeeds,
  14.     or things will get all screwed up.
  15.  
  16.     Also, there shouldn't be any double quotes (")
  17.     within the file except as string delimiters; i.e,
  18.     tabify shouldn't be used on itself because of the
  19.     quote in this sentence and the double quotes enclosed
  20.     within single quotes further down in the file.
  21.  
  22.     The input file isn't altered; the result is a new
  23.     file named by the second argument.
  24.  
  25.     The most common use of this program is to tabify
  26.     text files which you've loaded in over the phone
  27.     from a computer system (like UNIX) that tends to
  28.     turn all tabs into spaces for 300 baud DECwriters.
  29.  
  30. */
  31.  
  32. main(argc,argv)
  33. char **argv;
  34. {
  35.     int scount, column, ifd, ofd, i;
  36.     int c;
  37.     char ibuf[134], obuf[134];
  38.  
  39.     if (argc != 3) {
  40.         printf("usage: tabify oldfile newfile\n");
  41.         exit();
  42.     }
  43.     ifd = fopen(argv[1],ibuf);
  44.     ofd = fcreat(argv[2],obuf);
  45.     if (ifd == -1 || ofd == -1) {
  46.         printf("Can't open file(s)\n");
  47.         exit();
  48.     }
  49.  
  50.     scount = column = 0;
  51.  
  52.     do {
  53.         c = getc(ibuf);
  54.         if (c == -1) {
  55.             putc(0x1a,obuf);
  56.             break;
  57.          }
  58.         switch(c) {
  59.            case 0x0d:    putc1(c,obuf);
  60.                 scount = column = 0;
  61.                 break;
  62.            case 0x0a:    putc1(c,obuf);
  63.                 scount = 0;
  64.                 break;
  65.            case ' ':    column++;
  66.                 scount++;
  67.                 if (!(column%8)) {
  68.                    if (scount > 1)
  69.                     putc1('\t',obuf);
  70.                    else
  71.                     putc1(' ',obuf);
  72.                     scount = 0;
  73.                  }
  74.                 break;
  75.            case '\t':    scount = 0;
  76.                 column += (8-column%8);
  77.                 putc1('\t',obuf);
  78.                 break;
  79.             case '"':   for (i=0; i<scount; i++)
  80.                     putc1(' ',obuf);
  81.                 putc1('"',obuf);
  82.                 do {
  83.                    c = getc(ibuf);
  84.                    if (c == -1) {
  85.                     printf("Quote error.\n");
  86.                     exit();
  87.                    }
  88.                    putc1(c,obuf);
  89.                 } while (c != '"');
  90.                 do {
  91.                     c = getc(ibuf);
  92.                     putc1(c,obuf);
  93.                 } while (c != 0x0a);
  94.                 column = scount = 0;
  95.                 break;
  96.            case 0x1a:    putc(0x1a,obuf);
  97.                 break;
  98.            default:    for (i=0; i<scount; i++)
  99.                     putc1(' ',obuf);
  100.                 scount = 0;
  101.                 column++;
  102.                 putc1 (c,obuf);
  103.          }
  104.      } while (c != 0x1a);
  105.  
  106.     fflush(obuf);
  107.     close(obuf);
  108.     close(ibuf);
  109. }
  110.  
  111. putc1(c,buf)
  112. char c;
  113. {
  114.     putchar(c);
  115.     if (putc(c,buf) < 0) {
  116.         printf("\n\ntabify: disk write error.");
  117.         exit();
  118.     }
  119. }
  120.  
  121.